package com.adfframeworks.labs.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.adfframeworks.labs.dao.CommunityDAO;
import com.adfframeworks.labs.domain.Community;

@Repository
public class CommunityDAOImpl extends JdbcDaoSupport implements CommunityDAO {

	@Autowired
	CommunityDAOImpl(DataSource dataSource) {
		setDataSource(dataSource);
	}

	@Override
	public void create(String name, Integer owner, String keyword,
			Integer visibility) {
		String SQL = "INSERT INTO Community (Name, Owner, KeyWord, Visibility) VALUES (?, ?, ?, ?)";
		getJdbcTemplate().update(SQL,
				new Object[] { name, owner, keyword, visibility });
		System.out.println("CommunityDAOImpl.create(" + name + ", " + owner
				+ ", " + keyword + ", " + visibility + ")");
	}

	@Override
	public Community getCommunity(Integer id) {
		String SQL = "SELECT * FROM Community WHERE Community_ID = ?";
		Community community = (Community) getJdbcTemplate().queryForObject(SQL,
				new Object[] { id }, new CommunityMapper());
		return community;
	}

	@Override
	public Community getCommunity(String name) {
		String SQL = "SELECT * FROM Community WHERE Name = ?";
		Community community = (Community) getJdbcTemplate().queryForObject(SQL,
				new Object[] { name }, new CommunityMapper());
		return community;
	}

	@Override
	public List<Community> listTenLastCommunities() {
		String SQL = "SELECT * FROM Community WHERE Visibility > 0 ORDER BY Community_ID DESC LIMIT 10";
		List<Community> communities = getJdbcTemplate().query(SQL,
				new CommunityMapper());
		return communities;
	}

	@Override
	public List<Community> listAllCommunities() {
		String SQL = "SELECT * FROM Community";
		List<Community> communities = getJdbcTemplate().query(SQL,
				new CommunityMapper());
		return communities;
	}

	@Override
	public List<Community> listKeywordCommunities(String keyword) {
		String SQL = "SELECT * FROM Community WHERE KeyWord LIKE '%?%' AND Visibility > 0";
		List<Community> communities = getJdbcTemplate().query(SQL,
				new Object[] { keyword }, new CommunityMapper());
		return communities;
	}

	@Override
	public void delete(Integer id) {
		String SQL = "DELETE FROM Community WHERE Community_ID = ?";
		getJdbcTemplate().update(SQL, new Object[] { id });
		System.out.println("CommunityDAOImpl.delete(" + id + ")");
	}

	@Override
	public void delete(String name) {
		String SQL = "DELETE FROM Community WHERE Name = ?";
		getJdbcTemplate().update(SQL, new Object[] { name });
		System.out.println("CommunityDAOImpl.delete(" + name + ")");
	}

	@Override
	public void update(Integer id, String name, Integer owner, String keyword,
			Integer visibility) {
		String SQL = "UPDATE Community SET Name = ?, Owner = ?, KeyWord = ?, Visibility = ? WHERE Community_ID = ?";
		getJdbcTemplate().update(SQL,
				new Object[] { name, owner, keyword, visibility, id });
		System.out.println("CommunityDAOImpl.update(" + id + ", " + name + ", "
				+ owner + ", " + keyword + ", " + visibility + ")");
	}

}
